/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dll.h"
#include "Cursor.h"
#include <curses.h>

struct CSLP_CURSOR_POSITION {
  int y;
  int x;
};

struct CSLP_CURSOR_POSITION saved_cursor_pos = {0, 0};

///////////////////////////////////////////////////////////
/// Permet de placer le curseur en début de ligne.
///////////////////////////////////////////////////////////
void DLL_IMPEXP	CSLP_GotoBeginOfLine()
{
  int y, x;
  getyx(stdscr, y, x);
  move(y, 0);
}

///////////////////////////////////////////////////////////
/// Permet de placer le curseur à la ligne précédente
///////////////////////////////////////////////////////////
int DLL_IMPEXP CSLP_GotoPreviousLine()
{
  int y, x;
  getyx(stdscr, y, x);
  return move(y - 1, 0);
}

///////////////////////////////////////////////////////////
/// Permet de placer le curseur à la ligne suivante
///////////////////////////////////////////////////////////
int DLL_IMPEXP CSLP_GotoNextLine()
{
  int y, x;
  getyx(stdscr, y, x);
  return move(y + 1, 0);
}

///////////////////////////////////////////////////////////
/// Permet de déplacer le curseur dans la direction voulue
///////////////////////////////////////////////////////////
int DLL_IMPEXP CSLP_MoveCursor(CSLP_WAY way, unsigned int value)
{
  int ret_val, y, x;
  ret_val = ERR;
  getyx(stdscr, y, x);
  if (value != 0)
  {
    switch (way)
    {
      case FORWARD:
	ret_val = move(y, x + value); break;
      case BACKWARD:
	ret_val = move(y, x - value); break;
      case DOWN:
	ret_val = move(y + value, x); break;
      case UP:
	ret_val = move(y - value, x); break;
      default:
	fputs("CSLP_MoveCursor : argument way invalid.\n", stderr);
    }
  } // : if (value != 0)
  return ret_val;
}

///////////////////////////////////////////////////////////
/// Permet de sauvegarder la position courrente du curseur
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_SavePos()
{
  getyx(stdscr, saved_cursor_pos.y, saved_cursor_pos.x);
}

///////////////////////////////////////////////////////////
/// Permet de restaurer la position du curseur
///////////////////////////////////////////////////////////
void DLL_IMPEXP CSLP_RestorePos()
{
  move(saved_cursor_pos.y, saved_cursor_pos.x);
}

